﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;

namespace LiuYan.Interface
{
   public class JsApiPay
    {
       public void GetUnifiedOrderResult(UnifiedEntity entity)
       {
           //统一下单
           Dictionary<string, string> sParams = new Dictionary<string, string>();
           sParams.Add("body", entity.body);//商品描述
           sParams.Add("attach", "test");//附加数据
           sParams.Add("notify_url", "test");//通知地址
           sParams.Add("out_trade_no", entity.out_trade_no);//out_trade_no   商户系统内部的订单号
           sParams.Add("total_fee", entity.total_fee);//支付金额
           sParams.Add("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));//交易开始时间
           sParams.Add("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));//交易结束时间
           sParams.Add("goods_tag", entity.goods_tag);//商品标记
           sParams.Add("trade_type", "JSAPI");//支付方式
           sParams.Add("openid", entity.openid);//该用户的openid
           System.Net.WebClient wc = new System.Net.WebClient();//将xml数据post给微信统一下单接口
           string xml = new Common().CreateXML(sParams);//将数据转换成xml数据
           string returnmsg = wc.UploadString(PayHelperEntity.UnifiedPayUrl, "post", xml);//微信返回xml
           CreateLogo(returnmsg,"rz");
      
       }

        //<summary>
        //创建日志文件
        //</summary>
        //<param name="str"></param>
       public void CreateLogo(string str, string fileName)
       {
           FileStream fs = new FileStream(("@/"+fileName+".txt"), FileMode.Create, FileAccess.Write);
           StreamWriter sw = new StreamWriter(fs);
           //    sw.BaseStream.Seek(0, SeekOrigin.Begin);
           sw.WriteLine(str);
           sw.Close();
           fs.Close();
       }
    }
}
